//
//  GameCenter.m
//  MagicBubbles
//
//  Created by fisker on 11-4-3.
//  Copyright 2011 LegendSoftware. All rights reserved.
//

#import "GameCenter.h"
#import "GameKit/GameKit.h"
//#import "AchievementManager.h"


@interface GameCenterViewController : UIViewController {
}
@end

@implementation GameCenterViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
	return YES;
}
@end



@implementation GameCenter

static GameCenter* _sharedGameCenter = nil;

+(id)sharedGameCenter
{
	if (nil == _sharedGameCenter) {
		_sharedGameCenter = [[GameCenter alloc] init];
	}
	
	return _sharedGameCenter;
}

-(id)init
{
	if ((self = [super init])) {
		deviceSupported_ = [self isGameCenterAPIAvailable];
	}
	return self;
}

-(void)authenticatePlayer
{	
	if (deviceSupported_) {
		GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
		[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
			if (localPlayer.isAuthenticated)
			{
				//[[AchievementManager sharedAchievementManager]check];
			}
		}];
	}
}
-(void)reportScore:(int)score forCategory:(NSString*)category;
{
	NSLog(@"start report score %d\n", score);

	GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
	if (localPlayer.isAuthenticated) {
		GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
		scoreReporter.value = score;
	
		[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
			if (error != nil) {
				NSLog(@"reportScore failed: %@\n", [error localizedDescription]);
			} else {
				NSLog(@"reportScore success\n");
			}
		}];
	}
}

/*
-(void)reportAchievement:(NSString*)identifier percentComplete:(float)percent
{
	GKAchievement* achievement = [[[GKAchievement alloc]initWithIdentifier:identifier]autorelease];
	achievement.percentComplete = percent * 100;
	[achievement reportAchievementWithCompletionHandler:^(NSError *error) {}];
}
*/

-(BOOL)isAuthenticated
{
	if (!deviceSupported_) {
		return NO;
	}
	
	GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
	
	return localPlayer.isAuthenticated;
}

-(BOOL)isGameCenterAPIAvailable
{
	// Check for presence of GKLocalPlayer class.
    BOOL localPlayerClassAvailable = (NSClassFromString(@"GKLocalPlayer")) != nil;
	
    // The device must be running iOS 4.1 or later.
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
	
#ifdef EDIT_MODE
	return NO;
#endif
	
    return (localPlayerClassAvailable && osVersionSupported);
}

-(BOOL)enterLeaderboard:(NSString*)category withView:(UIView*)view {
	GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
	if (!localPlayer.isAuthenticated) {
		return NO;
	}
	
	if (gameCenterView) {
		return NO;
	}
	
	gameCenterView = [[GameCenterViewController alloc] init];
	gameCenterView.view = view;
	
	GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
	if (leaderboardController == nil) 
	{
		return NO;
	}
	
	leaderboardController.category = category;
	leaderboardController.leaderboardDelegate = self; 
	[gameCenterView presentModalViewController: leaderboardController animated: YES];
	return YES;
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController 
{
	[gameCenterView dismissModalViewControllerAnimated:YES];
	[gameCenterView release];
	gameCenterView = nil;
}

@end
